package com.dlc.shop.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dlc.shop.bean.enums.RefundStatusEnum;
import com.dlc.shop.bean.model.OrderRefund;
import com.dlc.shop.bean.model.OrderRefundIntervention;
import com.dlc.shop.common.constants.PlatformInterventionStatus;
import com.dlc.shop.common.exception.YamiShopBindException;
import com.dlc.shop.dao.OrderRefundInterventionMapper;
import com.dlc.shop.service.OrderRefundInterventionService;
import com.dlc.shop.service.OrderRefundService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 订单退款介入记录
 *
 * @author LGH
 * @date 2023-10-26 15:52:39
 */
@Service
@AllArgsConstructor
public class OrderRefundInterventionServiceImpl extends ServiceImpl<OrderRefundInterventionMapper, OrderRefundIntervention> implements OrderRefundInterventionService {

    private final OrderRefundInterventionMapper orderRefundInterventionMapper;

    private final OrderRefundService orderRefundService;

    @Override
    public void saveInterventionVoucher(OrderRefundIntervention orderRefundIntervention) {
        OrderRefund orderRefund = orderRefundService.getById(orderRefundIntervention.getRefundId());
        if (Objects.isNull(orderRefund)) {
            // 撤销失败 退款订单不存在
            throw new YamiShopBindException("yami.refund.order.not.exist");
        }
        if (Objects.equals(orderRefund.getReturnMoneySts(), RefundStatusEnum.SUCCEED.value())) {
            // 买家已发货，不能撤销退款申请
            throw new YamiShopBindException("yami.refund.succeed.no.submit.vouche");
        }
        if (!Objects.equals(orderRefund.getReturnMoneySts(), RefundStatusEnum.FAIL.value())) {
            // 卖家正在处理退款，不能撤销退款申请
            throw new YamiShopBindException("yami.shop.dispose.no.submit.voucher");
        }
        if (!Objects.equals(orderRefund.getPlatformInterventionStatus(), PlatformInterventionStatus.APPLY.value())) {
            throw new YamiShopBindException("yami.platform.involved.status.fail.no.sumit.voucher");
        }
        Boolean timeCheck = DateUtil.compare(DateUtil.offsetHour(orderRefund.getApplyInterventionTime(),72) , DateUtil.date()) < 0;
        if (timeCheck) {
            throw new YamiShopBindException("yami.added.voucher.time.no.add.voucher");
        }
        orderRefundInterventionMapper.insert(orderRefundIntervention);
    }
}
